import {Log} from "../../log/Log.js";
import {BaseGCG} from "../../BaseGCG.js";

/**
 * 控件移动
 */
export class Move extends BaseGCG{
    step = 1;//步长

    do(element){
        let that = this;
        let direction = that.getDirection(element);
        if(direction === 'up'){
            //按方位移动
            that.up(element);
        }
        if(direction === 'down'){
            //按方位移动
            that.down(element);
        }
        if(direction === 'left'){
            //按方位移动
            that.left(element);
        }
        if(direction === 'right'){
            //按方位移动
            that.right(element);
        }
    }

    /**
     *
     * @param element
     */
    init(element){
        Log.log('移动组件初始化');
        if(!element) throw new Error('移动元素不能为空');
        if(!this.class(element)) throw new Error('移动元素不能class不存在');
        if(!this.id(element)) throw new Error('移动元素不能ID不存在');



    }

    /**
     * 上
     * @param element
     */
    up(element){
        Log.log('向上移动');
        let that = this;
        if($(element).length>1){
            $(element).each(function (a, b) {
                $(b).css({top: (that.getTop(b) - that.step) + 'px'});//实时设置
            });
            return true;
        }
        $(element).css({top: (that.getTop(element) - that.step) + 'px'});//实时设置
        return true;
    }

    /**
     * 下
     * @param element
     */
    down(element){
        Log.log('向下移动');
        let that = this;
        if($(element).length>1){
            $(element).each(function (a, b) {
                $(b).css({top: (that.getTop(b) + that.step) + 'px'});//实时设置
            });
            return true;
        }
        $(element).css({top: (that.getTop(element) + that.step) + 'px'});//实时设置
        return true;
    }

    /**
     * 左
     * @param element
     */
    left(element){
        Log.log('向左移动');
        let that = this;
        if($(element).length>1){
            $(element).each(function (a, b) {
                $(b).css({left: (this.getLeft(b) - that.step) + 'px'});//实时设置
            });
            return true;
        }
        $(element).css({left: (this.getLeft(element) - this.step) + 'px'});//实时设置
        return true;
    }

    /**
     * 右
     * @param element
     */
    right(element){
        Log.log('向右移动');
        let that = this;
        if($(element).length>1){
            $(element).each(function (a, b) {
                $(b).css({left: (this.getLeft(b) + that.step) + 'px'});//实时设置
            });
            return true;
        }
        $(element).css({left: (this.getLeft(element) + this.step) + 'px'});//实时设置
        return true;
    }




}